/**
 * Copyright (c) 2002-2012 旭鸣信息
 * All rights reserved. 
 * File：UserAddrListDirective.java
 * History: Dec 27, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.directive;

import static freemarker.template.ObjectWrapper.DEFAULT_WRAPPER;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xteam.ecsystem.base.Constant;
import com.xteam.ecsystem.entities.BaseReceiveAddress;
import com.xteam.ecsystem.service.front.FrontUserAddrService;
import com.xteam.ecsystem.utils.DirectiveUtil;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * 会员地址列表 标签。
 * @author liuzhi
 */
@Service
public class UserAddrListDirective implements TemplateDirectiveModel {
	
	@Autowired
	private FrontUserAddrService userAddrService;

	@SuppressWarnings("unchecked")
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		// 获取登录用户id，以加载其对应的有效地址列表
		String bmiMember = DirectiveUtil.getString(params, Constant.PAR_MEMBERID.value());
		
		List<BaseReceiveAddress> listObj = this.userAddrService.getUserAddrs(bmiMember);
		
		env.setVariable(Constant.VAR_USERADDRLIST.value(),DEFAULT_WRAPPER.wrap(listObj));
		body.render(env.getOut());
	}
}
